package com.su.song.shapapplication;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.su.song.shapapplication.util.ViewUtil;

import org.w3c.dom.Text;

public class Shap2Activity extends AppCompatActivity {

    private TextView tv_result;
    private EditText de_phone;
    private EditText edp_password;
    private TextView tv_show;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shap2);
        RadioGroup rg_group = findViewById(R.id.rg_group);
        tv_result = findViewById(R.id.tv_result);
        rg_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.male:
                        String desc = "恭喜，您选择了男性";
                        tv_result.setText(desc);
                        break;
                    case R.id.female:
                        String desc2 = "恭喜，您选择了女性";
                        tv_result.setText(desc2);
                        break;
                    case R.id.copter:
                        String desc3 = "等等？您的性别似乎是直升机？";
                        tv_result.setText(desc3);
                }
            }
        });

//监听软键盘
        de_phone = findViewById(R.id.ed_phone);
        edp_password = findViewById(R.id.edp_password);
        edp_password.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    String phone = de_phone.getText().toString();//转化字符串，获取手机号
                    if(TextUtils.isEmpty(phone) || phone.length() < 11){
                        de_phone.requestFocus();//判断不符合条件，就将焦点返回到手机号输入框
                        //弹出不符合要求的提示
                        Toast.makeText(Shap2Activity.this, "请输入11号手机位", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

        de_phone.addTextChangedListener(new HideTextWater(de_phone,11));
        edp_password.addTextChangedListener(new HideTextWater(edp_password,6));//添加了文本监听器收起软件盘

//弹出框
        tv_show = findViewById(R.id.tv_show);
        findViewById(R.id.btn_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder= new AlertDialog.Builder(Shap2Activity.this);
                //对话标题文本
                builder.setTitle("尊敬的用户");
                builder.setMessage("你真的要登录么？");
                //肯定按钮
                builder.setPositiveButton("登录", (dialog, which) -> tv_show.setText("登录成功，尊敬的用户您已尊享黄金VIP用户权益"));
                //否定按钮
                builder.setNegativeButton("不登录", (dialog, which) -> {
                    tv_show.setText("尚未登录，您的权限不能生效");
                });
                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });


        //滑轮日期


    }

    private class HideTextWater implements TextWatcher {
        private EditText mView;//声明一个编辑框对象
        private int MaxtLength;//声明一个最大长度变量

        public HideTextWater(EditText v, int MaxLength) {
            this.mView = v;
            this.MaxtLength = MaxLength;//初始化
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }


        @Override
        public void afterTextChanged(Editable s) {
            String str = s.toString();
            if(str.length() == MaxtLength){
                ViewUtil.HideInputMethod(Shap2Activity.this,mView);
            }
        }
    }


}